<?php
// +------------------------------------------------------------------------------
// | TODO:自定义公共函数库--通用函数
// +-------------------------------------------------------------------------------
// | CreateTime:2016-01-01
// +-------------------------------------------------------------------------------
// | Author: JieHuiLong <jiehuilong@icloud.com> WebSite:<http://www.jiehuilong.com>
// +-------------------------------------------------------------------------------
/**
 * [getRoyalties 推广奖励金额、分成体系]
 * @param  integer $royalties [2~~10]
 * @return [type]             [Array]
 */
function getRoyalties($royalties = 6)
 {
     switch ($royalties) {
         case 2:
             $data =array(0.5,0.5);
             break;
         case 3:
             $data = array(0.4,0.4,0.2);
             break;
         case 4:
             $data = array(0.3,0.3,0.3,0.1);
             break;
         case 5:
             $data = array(0.3,0.3,0.2,0.15,0.05);
             break;
         case 6:
             $data = array(0.2,0.2,0.2,0.15,0.15,0.1);
             break;
         case 7:
             $data = array(0.2,0.2,0.15,0.15,0.15,0.1,0.05);
             break;
         case 8:
             $data = array(0.2,0.2,0.15,0.15,0.1,0.1,0.05,0.05);
             break;
         case 9:
             $data = array(0.2,0.15,0.15,0.15,0.1,0.1,0.05,0.05,0.05);
             break;
         case 10:
             $data = array(0.15,0.15,0.15,0.15,0.1,0.1,0.05,0.05,0.05,0.05);
             break;
         default:
             return false;
             break;
     }
     return $data;
 } 
/**
 * 获取字符串中的数字
 * @param  string $string [字符串]
 * @param  string $type   [是否获取所有的数字集合 single/all]
 * @return [type]         [int/array]
 */
function getNumberForString($string='',$type='single')
{
    $string = str_replace(' ','',$string);
    if($type=='single'){
        $item = preg_match('/\d+/',$string,$arr);
        return (int)$arr['0'];
    }else{
        $item = preg_match_all('/\d+/',$string,$arr);
        $data['count'] = $item;
        $data['listArr'] = $arr;
        return $data;
    }
}

/***
 *生成商品订单号
 *@author JieHuiLong <jiehuilong@icloud.com UpateTime 2014-03-19>
 *@return string example:www.baidu.com
*/
function createOrderNo() {
    return date('Ymd'). substr(time(), -4) . substr(microtime(), 2, 3) . sprintf(rand(10, 99));
}
/**
 * 获取对应支付状态的文字信息
 * @param int $status
 * @return string 状态文字 ，false 未获取到
 * @author JieHuiLong <jiehuilong@icloud.com>
 */
function getPayTitle($status = null) {
    if (!isset($status)) {
        return false;
    }
    switch ($status) {
        case -1:
            return '取消订单';
            break;
        case 10:
            return '等待付款';
            break;
        case 20:
            return '等待发货';
            　break;
        case 30:
            return '等待收货';
            break;
        case 40:
            return '交易完成';
            　break;
        default:
            return false;
            break;
    }
}

/**
 * 判断域名是否是官方域名
 * @return true/false
 */
function checkDomain($domain) {
    if (strpos($domain, "shinte.cn") > -1 or strpos($domain, "xn--o2o-ts1kfp.cn") > -1 or strpos($domain, "餐饮o2o.cn") > -1 or strpos($domain, "fi020.cn") > -1 or strpos($domain, "xn--020-ts1kfp.cn") or strpos($domain, "餐饮020.cn") or strpos($domain, "allo2o.com") > -1) {
        return true;
    }else {
        return false;
    }
}
/**
 * 判断用户来源页面
 * @return true/false
 */
function checkReferer() {
    $referer = $_SERVER['HTTP_REFERER'];
    if (strpos($referer, "shinte.cn") != false or strpos($referer, "餐饮o2o.cn") != false or strpos($referer, "gdgzch.com") != false or strpos($referer, "allo2o.com") != false) {
        return true;
    }
    else {
        return false;
    }
}
/**
 * 根据id得省名、城市名、地区名。。。
 * @param  $id DistrictD
 * @return string 省名
 */
function getDistrictName($id) {
    $res = M('district')->where(array('id' => $id))->find();
    return $res['name'];
}

/**
 * 生成手机验证码
 * @param  integer $length  [验证码长度]
 * @param  integer $numeric [验证码的类型，0：生成字母验证码，其他生成整数生成数字验证码]
 * @return [type]           [description]
 */
function getMobileVerify($length = 6 , $numeric = 0) {
    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    if($numeric) {
        $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
    } else {
        $hash = '';
        $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
        $max = strlen($chars) - 1;
        for($i = 0; $i < $length; $i++) {
            $hash .= $chars[mt_rand(0, $max)];
        }
    }
    return $hash;
}
//判断是否是网址
function isUrl($url='')
{
    $regex = '@(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))@';
    return preg_match($regex,$url);
}
/**
 * [isMobile 判断手机还是PC访问--Start]
 * @return boolean [false/true]
 * @Mender: JieHuiLong <jiehuilong@icloud.com>
 * @UpdateTime: 2016-01-14 14:24:21
 * @UpdateDescription: Description
 */
function isMobile(){
    $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';
    $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');
    $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');
    $found_mobile=checkSubstrs($mobile_os_list,$useragent_commentsblock) || checkSubstrs($mobile_token_list,$useragent);
    if ($found_mobile){
        return true;
    }else{
        return false;
    }
}
function checkSubstrs($substrs,$text){
    foreach($substrs as $substr)
        if(false!==strpos($text,$substr)){
            return true;
        }
        return false;
}
//判断手机还是PC访问--End

/**
 * [time_later description]
 * @param  [type] $time [description]
 * @return [type]       [description]
 * @Mender: JieHuiLong <jiehuilong@icloud.com>
 * @UpdateTime: 2016-01-14 14:22:58
 */
function timeLater($time) {
    $t = $time - time();
    //与当前时间比较，计算剩余的毫秒数
    if ($t <= 3600) {
        // 不要给太精确的。一者没必要，二者减轻MySQL触发器的性能消耗，设为20min扫描一次表，而不是每分钟都扫一次
        return '约1小时内';
    }
    elseif ($t > 3600 && $t <= 86400) {
        return floor($t / 3600) . '小时后';
    }
    elseif ($t > 86400 && $t <= 2592000) {
        return floor($t / 86400) . '天后';
    }
    elseif ($t > 2592000 && $t <= 31104000) {
        return floor($t / 2592000) . '个月后';
    }
    else {
        $t = floor($t / 2592000);
        return floor($t / 12) . '年' . floor($t % 12) . '个月后';
    }
}
/**
 * 模板输出的时候，把毫秒数化为【多少秒/分钟/小时】前
 * @param  int(10) $time 毫秒数
 * @return string XXX[秒/分钟/小时] 前
 * @Mender: JieHuiLong <jiehuilong@icloud.com>
 * @UpdateTime: 2016-01-14 14:23:32
 */
function timeAgo($time){
    $now = time();
    $t = $now - $time;
    if ( $t <= 60 ) {
        return '刚刚';
    }
    elseif ( ($t > 60) &&($t <= 3600) ) {
        return floor( $t / 60 ) .'分钟前';
    }
    elseif ( ($t > 3600) && (date('d', $time) === date('d', $now)) ) {
        return '今天 '.date('H:i', $time);
    }
    elseif ( ($t < 172800) && (date('d',$time + 86400) === date('d', $now)) ) {
        return '昨天 '.date('H:i', $time);
    }
    else {
        return date('y/m/d/ H:i', $time);
    }
}

/**
 *  随机数
 * @author ZhuXiaoYan  <346107105@qq.com>
 * @DateTime 2015-12-14T13:02:13+0800
 * @param $max 最大值
 * @param $min 最小值
 * @param $num 生成数量
 * @return array 生成的随机一位数组
 */
function getRand($max, $min, $num)
{
    $count=0;
    while ($count < $num) {
        $numrd[]=mt_rand($min,$max);
        $numfl=array_flip(array_flip($numrd));
        $count=count($numfl);
    }
    shuffle($numfl);
    return $numfl;
}
/**
 * 计算2个时间的时间差。返回格式如 n天n时n分
 * @author 朱家言 <346107105@qq.com>
 * @DateTime 2015-12-15T18:14:53+0800
 * @param    [type]                   $start 开始时间
 * @param    [type]                   $end   结束时间
 * @return   [type]                          时间差值(n天n时n分)
 */
function diffToTime($start,$end,$isstr=true)
{
    $cle = $end - $start;
    if($isstr){
        $d = floor($cle/3600/24);
        $h = floor(($cle%(3600*24))/3600);
        $m = floor(($cle%(3600*24))%3600/60);
        $str='';
        if ($d>0) $str.=$d.'天';
        if ($h>0) $str.=$h.'小时';
        if ($m>0) $str.=$m.'分钟';
        return $str;
    }else{
        return $cle;
    }
}
